package blog.service.impl;

import blog.dao.*;
import blog.pojo.*;
import blog.service.ChapterService;
import blog.util.Timestamp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ChapterServiceImpl implements ChapterService {

    @Autowired
    private ChapterDao chapterDao;

    @Autowired
    private ChapterRecordDao chapterRecordDao;

    @Autowired
    private ChapterChapterLabelDao chapterChapterLabelDao;

    @Autowired
    private ChapterChapterCategoryDao chapterChapterCategoryDao;

    @Autowired
    private ChapterCategoryDao chapterCategoryDao;

    @Autowired
    private ConfigDao configDao;

    @Override
    public List<Chapter> getIndexCommendList() throws Exception {
        Config config = new Config().setName(Config.HOME_COMMEND_CHAPTER_NUMBER);
        config = configDao.selectOne(new QueryWrapper<>(config));
        QueryWrapper wrapper = new QueryWrapper<Chapter>()
                .orderByAsc("serial")
                .last("limit " + config.getValue());
        List<Chapter> list = chapterDao.selectList(wrapper);
        return list;
    }

    @Override
    public Page<Chapter> getPage(Integer page, Integer limit, Integer categoryId, Integer labelId) throws Exception {
        Page<Chapter> chapterPage = new Page<Chapter>()
                .setCurrent(page)
                .setSize(limit);
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        if(categoryId != null){
            QueryWrapper<ChapterChapterCategory> temp = new QueryWrapper<ChapterChapterCategory>()
                    .setEntity(new ChapterChapterCategory().setChapterCategoryId(categoryId));
            List<ChapterChapterCategory> chapterChapterCategories = chapterChapterCategoryDao.selectList(temp);
            if(chapterChapterCategories.size() == 0){
                return chapterPage.setTotal(0).setRecords(new ArrayList<>());
            }
            List<Integer> chapterIdList = new ArrayList<>();
            for(ChapterChapterCategory chapterChapterCategory : chapterChapterCategories){
                chapterIdList.add(chapterChapterCategory.getChapterId());
            }
            wrapper.in("id", chapterIdList);
        }
        if(labelId != null){
            QueryWrapper<ChapterChapterLabel> temp = new QueryWrapper<ChapterChapterLabel>()
                    .setEntity(new ChapterChapterLabel().setChapterLabelId(labelId));
            List<ChapterChapterLabel> chapterChapterLabelList = chapterChapterLabelDao.selectList(temp);
            if(chapterChapterLabelList.size() == 0){
                return chapterPage.setRecords(new ArrayList<>()).setTotal(0);
            }
            List<Integer> chapterIdList = new ArrayList<>();
            for (ChapterChapterLabel chapterChapterLabel : chapterChapterLabelList){
                chapterIdList.add(chapterChapterLabel.getChapterId());
            }
            wrapper.in("id", chapterIdList);
        }

        return chapterDao.selectPage(chapterPage, wrapper);
    }

    @Override
    public Page<Chapter> getPage(Page page, Chapter chapter, String keywords) throws Exception {
        if (page == null) return null;
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>(chapter)
                .ne("isuse", 2);
        if (keywords != null) {
            queryWrapper.and(Wrapper -> Wrapper.like("title", keywords).or().like("content", keywords));
        }
        if (chapter.getCategoryId() != null) {
            List<Integer> categoryIdList = new ArrayList<>();
            categoryIdList.add(chapter.getCategoryId());
            List<ChapterCategory> chapterCategoryList = chapterCategoryDao.selectList(new QueryWrapper<>(new ChapterCategory().setPid(chapter.getCategoryId())));
            if (chapterCategoryList.size() > 0) {
                for (ChapterCategory chapterCategory : chapterCategoryList) {
                    categoryIdList.add(chapterCategory.getId());
                }
            }
            List<ChapterChapterCategory> chapterChapterCategoryList = chapterChapterCategoryDao.selectList(new QueryWrapper<ChapterChapterCategory>().in("chapter_category_id", categoryIdList));
            List<Integer> chapterIdList = new ArrayList<>();
            for(ChapterChapterCategory chapterChapterCategory : chapterChapterCategoryList) {
                chapterIdList.add(chapterChapterCategory.getChapterId());
            }
            if(chapterIdList.size() == 0) {
                return page.setRecords(new ArrayList()).setTotal(0);
            }
            queryWrapper.in("chapter_id", chapterIdList);
        }
        return chapterDao.selectPage(page, queryWrapper);
    }

    @Override
    public Chapter getInfo(Integer id) throws Exception {
        return chapterDao.selectOne(new QueryWrapper<Chapter>().setEntity(new Chapter().setId(id)));
    }

    @Override
    public Boolean update(Chapter chapter) throws Exception {
        chapter.setGmtUpdate(Timestamp.gen());
        return chapterDao.updateById(chapter) > 0;
    }

    @Override
    public Integer insertGetId(Chapter chapter) throws Exception {
        chapter.setGmtCreate(Timestamp.gen()).setGmtUpdate(Timestamp.gen());
        if (chapterDao.insert(chapter) > 0) {
            return chapter.getId();
        } else {
            return 0;
        }
    }

    @Override
    public Integer selectCountByCategoryId(Integer category_id) throws Exception {
        int num = 0;
        ChapterChapterCategory chapterChapterCategory = new ChapterChapterCategory().setChapterCategoryId(category_id);
        QueryWrapper<ChapterChapterCategory> wrapper = new QueryWrapper<>(chapterChapterCategory);
        for (ChapterChapterCategory temp : chapterChapterCategoryDao.selectList(wrapper)){
            QueryWrapper<Chapter> wrapper1 = new QueryWrapper<Chapter>()
                    .setEntity(new Chapter().setId(temp.getChapterId()))
                    .ne("isuse", 2);
            if(chapterDao.selectCount(wrapper1) > 0){
                num++;
            }
        }
        return num;
    }
}
